package com.hnxxxy.workshop.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnxxxy.workshop.mapper.DepotMapper;
import com.hnxxxy.workshop.model.DepotDO;
import com.hnxxxy.workshop.request.DepotPageRequest;
import com.hnxxxy.workshop.request.DepotRequest;
import com.hnxxxy.workshop.service.DepotService;
import com.hnxxxy.workshop.vo.DepotVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 仓库 服务实现类
 * </p>
 *
 * @author zqh
 * @since 2022-05-09
 */
@Service
@Slf4j
public class DepotServiceImpl implements DepotService {

    @Resource
    private DepotMapper depotMapper;

    /**
     * 新增仓库
     * @param depotRequest
     */
    @Override
    public void add(DepotRequest depotRequest) {

        DepotDO depotDO = new DepotDO();
        BeanUtils.copyProperties(depotRequest,depotDO);

        int rows = depotMapper.insert(depotDO);

        log.info("新增消息:rows={},data={}",rows,depotDO);
    }

    /**
     * 根据id删除仓库
     * @param id
     */
    @Override
    public void deleteById(int id) {
        int rows = depotMapper.deleteById(id);

        log.info("删除消息条数 :rows={}",rows);
    }

    /**
     * 修改仓库信息
     * @param depotRequest
     */
    @Override
    public void update(DepotRequest depotRequest) {
        DepotDO depotDO = new DepotDO();
        BeanUtils.copyProperties(depotRequest, depotDO);

        int rows = depotMapper.updateById(depotDO);
        log.info("修改消息:rows={},data={}",rows,depotDO);
    }

    /**
     * 根据id查询仓库信息
     * @param id
     * @return
     */
    @Override
    public DepotVO findByIdDepot(int id) {
        DepotVO depotVO = new DepotVO();
        DepotDO depotDO = depotMapper.selectById(id);

        BeanUtils.copyProperties(depotDO, depotVO);
        log.info("根据id查询信息:id={}  depotVO={}",id,depotVO);
        return depotVO;
    }

    /**
     * 分页条件查询
     * @param depotPageRequest
     * @return
     */
    @Override
    public Map<String, Object> page(DepotPageRequest depotPageRequest) {

        int page = depotPageRequest.getPage();
        int size = depotPageRequest.getSize();

        String name = depotPageRequest.getName();
        String director = depotPageRequest.getDirector();

        QueryWrapper<DepotDO> queryWrapper = new QueryWrapper<DepotDO>();

        if (!StrUtil.hasEmpty(name)){
            queryWrapper.like("name", name);
        }
        if (!StrUtil.hasEmpty(director)){
            queryWrapper.like("director", director);
        }

        Page<DepotDO> pageInfo = new Page<>(page, size);

        IPage<DepotDO> depotDOIPage = depotMapper.selectPage(pageInfo,queryWrapper);

        Map<String,Object> pageMap = new HashMap<>(3);

        pageMap.put("total_record", depotDOIPage.getTotal());
        pageMap.put("total_page", depotDOIPage.getPages());
        pageMap.put("current_data",depotDOIPage.getRecords()
                .stream().map(obj->beanProocess(obj)).collect(Collectors.toList()));

        log.info("分页消息:pageMap={}",pageMap);
        return pageMap;

    }

    private DepotVO beanProocess(DepotDO obj) {
        DepotVO depotVO = new DepotVO();
        BeanUtils.copyProperties(obj, depotVO);
        return depotVO;
    }


}
